#delimit;
** REPLACE FILE PATH WITH PATH TO RELEVANT REPLICATION FILES;
local fileloc = "~/KMS_REPLICATION";
set logtype text;
capture log close daily_to_weekly_pollution;

log using `fileloc'/log_files/daily_to_weekly_pollution.txt, name(daily_to_weekly_pollution) replace;
set more off;
clear all;

pause on;

set mem 400m;

**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
**XXXXXXXXXXXXXXXXX BUILDING WEEKLY  POLLUTION VALUES XXXXXXXXXXXXXXXXXXXX;
**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;

use `fileloc'/data/emissions_data/daily_all_pollutants.dta, clear;

gen week = wofd(date);
format week %tw;
gen year = year(date);

** Collapse daily values to weekly values;
collapse weekly_co = comax8hr  weekly_oz = ozmax8hr weekly_pm10 = pm10 year, by(location week) fast;

order location week weekly_co weekly_oz weekly_pm10;
sort location week;

** CREATE BALANCED PANEL ;
** Note our balance here begins in 2001, despite analysis beginning in 2002. This allows for trimester exposure measures, as infants born in early 2002 were exposed to pollution levels from 2001;
keep if year >= 2001 & year <= 2007;

** accounts for carbon monoxide, particulate matter (10), and ozone. We do not include ozone in the most recent analysis, but maintain this for reference to earlier NBER version;

** Restrict to those present in all years;
foreach pollutant in weekly_co weekly_oz weekly_pm10 {;
	forvalues year = 2001/2007 {;
		gen `pollutant'_`year' = (`pollutant' ~= . & year == `year');
		egen `pollutant'_`year'_around = max(`pollutant'_`year'), by(location);		
		drop `pollutant'_`year';
	};
	replace `pollutant' = . if `pollutant'_2001_around == 0 | `pollutant'_2002_around == 0 | `pollutant'_2003_around == 0 | `pollutant'_2004_around == 0 | `pollutant'_2005_around == 0 | `pollutant'_2006_around == 0 | `pollutant'_2007_around == 0 ;

	drop *_around;

};

keep if weekly_co ~= . | weekly_oz ~= . | weekly_pm10 ~= .;

** How many sensors?;
gsort location, g(howmany);
sum howmany;
drop howmany;

save `fileloc'/data/emissions_data/weekly_all_pollutants_KMS.dta, replace;

log close daily_to_weekly_pollution; 


